package com.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

public class Exceptions
{
  public static RuntimeException unchecked(Exception e)
  {
    if ((e instanceof RuntimeException)) {
      return (RuntimeException)e;
    }
    return new RuntimeException(e);
  }
  
  public static String getStackTraceAsString(Throwable e)
  {
    if (e == null) {
      return "";
    }
    StringWriter stringWriter = new StringWriter();
    e.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
  }
  
  public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses)
  {
    Throwable cause = ex.getCause();
    while (cause != null)
    {
      Class[] arrayOfClass;
      int j = (arrayOfClass = causeExceptionClasses).length;
      for (int i = 0; i < j; i++)
      {
        Class<? extends Exception> causeClass = arrayOfClass[i];
        if (causeClass.isInstance(cause)) {
          return true;
        }
      }
      cause = cause.getCause();
    }
    return false;
  }
}

